<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>kinect获取三维坐标 | 花椒茴香的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Kinect获取 骨骼图骨骼三维坐标该项目需要我们利用KinectV2 获取骨骼图并得到头部坐标，每一秒取一帧，一共五分钟，将坐标输出。 KinectV2 可以获得色彩图、深度图、骨骼图等等，其实一开始我也不知道该从哪里下手，但对骨骼图的代码进行了一下分析找到了突破点。 对KinectV2大体了解：Kinect一共有三个坐标空间，分别是相机空间、深度空间、色彩空间，相机空间对应骨骼图等，深度空间对">
<meta property="og:type" content="article">
<meta property="og:title" content="kinect获取三维坐标">
<meta property="og:url" content="http://huajiaohuixiang.gitee.io/2021/10/25/kinect%E8%8E%B7%E5%8F%96%E4%B8%89%E7%BB%B4%E5%9D%90%E6%A0%87/index.html">
<meta property="og:site_name" content="花椒茴香的博客">
<meta property="og:description" content="Kinect获取 骨骼图骨骼三维坐标该项目需要我们利用KinectV2 获取骨骼图并得到头部坐标，每一秒取一帧，一共五分钟，将坐标输出。 KinectV2 可以获得色彩图、深度图、骨骼图等等，其实一开始我也不知道该从哪里下手，但对骨骼图的代码进行了一下分析找到了突破点。 对KinectV2大体了解：Kinect一共有三个坐标空间，分别是相机空间、深度空间、色彩空间，相机空间对应骨骼图等，深度空间对">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-10-25T07:16:17.000Z">
<meta property="article:modified_time" content="2021-11-24T14:51:47.207Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Kinect">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="花椒茴香的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">花椒茴香的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://huajiaohuixiang.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-kinect获取三维坐标" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/10/25/kinect%E8%8E%B7%E5%8F%96%E4%B8%89%E7%BB%B4%E5%9D%90%E6%A0%87/" class="article-date">
  <time class="dt-published" datetime="2021-10-25T07:16:17.000Z" itemprop="datePublished">2021-10-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      kinect获取三维坐标
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="Kinect获取-骨骼图骨骼三维坐标"><a href="#Kinect获取-骨骼图骨骼三维坐标" class="headerlink" title="Kinect获取 骨骼图骨骼三维坐标"></a>Kinect获取 骨骼图骨骼三维坐标</h2><p>该项目需要我们利用KinectV2 获取骨骼图并得到头部坐标，每一秒取一帧，一共五分钟，将坐标输出。</p>
<p>KinectV2 可以获得色彩图、深度图、骨骼图等等，其实一开始我也不知道该从哪里下手，但对骨骼图的代码进行了一下分析找到了突破点。</p>
<h4 id="对KinectV2大体了解："><a href="#对KinectV2大体了解：" class="headerlink" title="对KinectV2大体了解："></a>对KinectV2大体了解：</h4><p>Kinect一共有三个坐标空间，分别是相机空间、深度空间、色彩空间，相机空间对应骨骼图等，深度空间对应深度图，色彩空间对应色彩图，并且KInectSDK中有CoordinateMapper类来实现深度空间到相机空间和深度空间到色彩空间的转换。<br>具体查看官方文档：<a target="_blank" rel="noopener" href="https://docs.microsoft.com/en-us/previous-versions/windows/kinect/dn785530(v=ieb.10)">https://docs.microsoft.com/en-us/previous-versions/windows/kinect/dn785530(v=ieb.10)</a></p>
<p>重点需要关心获取的帧的数据如何存储的，对于骨骼图，SDK中有两个重要的类分别是IBody 和 Joint 分别代表人和骨骼点。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">IBody : public IUnknown</span><br><span class="line">  &#123;</span><br><span class="line">  public:</span><br><span class="line">      virtual HRESULT STDMETHODCALLTYPE GetJoints( </span><br><span class="line">          /* [annotation] */ </span><br><span class="line">          _Pre_equal_to_(JointType_Count)  UINT capacity,</span><br><span class="line">          /* [annotation][size_is][out] */ </span><br><span class="line">          _Out_writes_all_(capacity)  Joint *joints) = 0;</span><br><span class="line">      </span><br><span class="line">    ...</span><br><span class="line">      virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_IsTracked( </span><br><span class="line">          /* [annotation][out][retval] */ </span><br><span class="line">          _Out_  BOOLEAN *tracked) = 0;</span><br><span class="line">    ...</span><br><span class="line">   </span><br><span class="line">     </span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure>

<p>分别利用GetJoints函数和get_IsTracked函数可以获得Joints和判断是否被记录到了。</p>
<p>对于Joints</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#ifndef _Joint_</span><br><span class="line">#define _Joint_</span><br><span class="line">typedef struct _Joint</span><br><span class="line">    &#123;</span><br><span class="line">    JointType JointType;</span><br><span class="line">    CameraSpacePoint Position;</span><br><span class="line">    TrackingState TrackingState;</span><br><span class="line">    &#125; 	Joint;</span><br><span class="line"></span><br><span class="line">#endif // _Joint_</span><br></pre></td></tr></table></figure>

<p>一个struct 里面有Joint类型、相机空间位置、追踪状态（是否被观测到），接着往里看CameSpacePoint</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#ifndef _CameraSpacePoint_</span><br><span class="line">#define _CameraSpacePoint_</span><br><span class="line">typedef struct _CameraSpacePoint</span><br><span class="line">    &#123;</span><br><span class="line">    float X;</span><br><span class="line">    float Y;</span><br><span class="line">    float Z;</span><br><span class="line">    &#125; 	CameraSpacePoint;</span><br><span class="line"></span><br><span class="line">#endif // _CameraSpacePoint_</span><br></pre></td></tr></table></figure>

<p>到这里就很清楚了，直接就可以得到相机空间地址了。（本来我以为需要先获得深度图地址，然后自己进行坐标计算的。。。）</p>
<p>然后SDK还定义了一下常量和其他类，在这里贴一下以防看代码的时候不懂</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">enum _JointType</span><br><span class="line">    &#123;</span><br><span class="line">        JointType_SpineBase	= 0,</span><br><span class="line">        JointType_SpineMid	= 1,</span><br><span class="line">        JointType_Neck	= 2,</span><br><span class="line">        JointType_Head	= 3,</span><br><span class="line">        JointType_ShoulderLeft	= 4,</span><br><span class="line">        JointType_ElbowLeft	= 5,</span><br><span class="line">        JointType_WristLeft	= 6,</span><br><span class="line">        JointType_HandLeft	= 7,</span><br><span class="line">        JointType_ShoulderRight	= 8,</span><br><span class="line">        JointType_ElbowRight	= 9,</span><br><span class="line">        JointType_WristRight	= 10,</span><br><span class="line">        JointType_HandRight	= 11,</span><br><span class="line">        JointType_HipLeft	= 12,</span><br><span class="line">        JointType_KneeLeft	= 13,</span><br><span class="line">        JointType_AnkleLeft	= 14,</span><br><span class="line">        JointType_FootLeft	= 15,</span><br><span class="line">        JointType_HipRight	= 16,</span><br><span class="line">        JointType_KneeRight	= 17,</span><br><span class="line">        JointType_AnkleRight	= 18,</span><br><span class="line">        JointType_FootRight	= 19,</span><br><span class="line">        JointType_SpineShoulder	= 20,</span><br><span class="line">        JointType_HandTipLeft	= 21,</span><br><span class="line">        JointType_ThumbLeft	= 22,</span><br><span class="line">        JointType_HandTipRight	= 23,</span><br><span class="line">        JointType_ThumbRight	= 24,</span><br><span class="line">        JointType_Count	= ( JointType_ThumbRight + 1 ) </span><br><span class="line">    &#125; ;</span><br><span class="line">#endif // _JointType_</span><br><span class="line"></span><br><span class="line">#ifndef _TrackingState_</span><br><span class="line">#define _TrackingState_</span><br><span class="line">typedef enum _TrackingState TrackingState;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">enum _TrackingState</span><br><span class="line">    &#123;</span><br><span class="line">        TrackingState_NotTracked	= 0,</span><br><span class="line">        TrackingState_Inferred	= 1,</span><br><span class="line">        TrackingState_Tracked	= 2</span><br><span class="line">    &#125; ;</span><br><span class="line">#endif // _TrackingState_</span><br><span class="line"></span><br><span class="line"> IBodyFrame : public IUnknown</span><br><span class="line">    &#123;</span><br><span class="line">    public:</span><br><span class="line">        virtual HRESULT STDMETHODCALLTYPE GetAndRefreshBodyData( </span><br><span class="line">            UINT capacity,</span><br><span class="line">            /* [annotation][size_is][out][in] */ </span><br><span class="line">            _Inout_updates_all_(capacity)  IBody **bodies) = 0;</span><br><span class="line">        </span><br><span class="line">        virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_FloorClipPlane( </span><br><span class="line">            /* [annotation][out][retval] */ </span><br><span class="line">            _Out_  Vector4 *floorClipPlane) = 0;</span><br><span class="line">        </span><br><span class="line">        virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_RelativeTime( </span><br><span class="line">            /* [annotation][out][retval] */ </span><br><span class="line">            _Out_  TIMESPAN *relativeTime) = 0;</span><br><span class="line">        </span><br><span class="line">        virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_BodyFrameSource( </span><br><span class="line">            /* [annotation][out][retval] */ </span><br><span class="line">            _COM_Outptr_  IBodyFrameSource **bodyFrameSource) = 0;</span><br><span class="line">        </span><br><span class="line">    &#125;;</span><br></pre></td></tr></table></figure>

<p>下面是项目代码的详细步骤：</p>
<p>第一步需要安装Kinect 的SDK，然后再Visual Studio进行项目配置，具体可以参考一下文章：</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/lizhiguo18/article/details/51037672">https://blog.csdn.net/lizhiguo18/article/details/51037672</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_43850620/article/details/103874847">https://blog.csdn.net/weixin_43850620/article/details/103874847</a></p>
<p>对项目配置好后就可以写代码啦。</p>
<p>1.这一步应该是获取Kinect，然后打开。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">IKinectSensor   * mySensor = nullptr;</span><br><span class="line">GetDefaultKinectSensor(&amp;mySensor);</span><br><span class="line">mySensor-&gt;Open();</span><br></pre></td></tr></table></figure>

<p>2.准备读取ColorFrame</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">   IColorFrameSource   * myColorSource = nullptr;</span><br><span class="line">mySensor-&gt;get_ColorFrameSource(&amp;myColorSource);</span><br><span class="line">IColorFrameReader   * myColorReader = nullptr;</span><br><span class="line">myColorSource-&gt;OpenReader(&amp;myColorReader);</span><br><span class="line">int colorHeight = 0, colorWidth = 0;</span><br><span class="line">IFrameDescription   * myDescription = nullptr;</span><br><span class="line">myColorSource-&gt;get_FrameDescription(&amp;myDescription);</span><br><span class="line">myDescription-&gt;get_Height(&amp;colorHeight);</span><br><span class="line">myDescription-&gt;get_Width(&amp;colorWidth);</span><br><span class="line">IColorFrame * myColorFrame = nullptr;</span><br><span class="line">Mat original(colorHeight, colorWidth, CV_8UC4);</span><br></pre></td></tr></table></figure>

<p>3.准备读取BodyFrame</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">   IBodyFrameSource    * myBodySource = nullptr;</span><br><span class="line">mySensor-&gt;get_BodyFrameSource(&amp;myBodySource);</span><br><span class="line">IBodyFrameReader    * myBodyReader = nullptr;</span><br><span class="line">myBodySource-&gt;OpenReader(&amp;myBodyReader);</span><br><span class="line">int myBodyCount = 0;</span><br><span class="line">myBodySource-&gt;get_BodyCount(&amp;myBodyCount);</span><br><span class="line">IBodyFrame  * myBodyFrame = nullptr;</span><br><span class="line">ICoordinateMapper   * myMapper = nullptr;</span><br><span class="line">mySensor-&gt;get_CoordinateMapper(&amp;myMapper);</span><br></pre></td></tr></table></figure>

<p>4.每30帧获取一帧然后输出</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">   int fps = 0;</span><br><span class="line">while (1)</span><br><span class="line">&#123;</span><br><span class="line">	while (myColorReader-&gt;AcquireLatestFrame(&amp;myColorFrame) != S_OK);</span><br><span class="line">	myColorFrame-&gt;CopyConvertedFrameDataToArray(colorHeight * colorWidth * 4, original.data, ColorImageFormat_Bgra);</span><br><span class="line">	Mat copy = original.clone();        //读取彩色图像并输出到矩阵</span><br><span class="line">	while (myBodyReader-&gt;AcquireLatestFrame(&amp;myBodyFrame) != S_OK); //读取身体图像</span><br><span class="line">	IBody   **  myBodyArr = new IBody *[myBodyCount];       //为存身体数据的数组做准备</span><br><span class="line">	if (fps != 0) &#123;</span><br><span class="line">		fps++;</span><br><span class="line">		fps = fps % 30;</span><br><span class="line">		delete[]myBodyArr;</span><br><span class="line">		myBodyFrame-&gt;Release();</span><br><span class="line">		myColorFrame-&gt;Release();</span><br><span class="line">		continue;</span><br><span class="line">	&#125;</span><br><span class="line">	fps++;</span><br><span class="line">	for (int i = 0; i &lt; myBodyCount; i++)</span><br><span class="line">		myBodyArr[i] = nullptr;</span><br><span class="line">	if (myBodyFrame-&gt;GetAndRefreshBodyData(myBodyCount, myBodyArr) == S_OK)     //把身体数据输入数组</span><br><span class="line">		for (int i = 0; i &lt; myBodyCount; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			BOOLEAN     result = false;</span><br><span class="line">			if (myBodyArr[i]-&gt;get_IsTracked(&amp;result) == S_OK &amp;&amp; result) //先判断是否侦测到</span><br><span class="line">			&#123;</span><br><span class="line">				Joint   myJointArr[JointType_Count];</span><br><span class="line">				if (myBodyArr[i]-&gt;GetJoints(JointType_Count, myJointArr) == S_OK)  </span><br><span class="line">				&#123;</span><br><span class="line">					draw(copy, myJointArr[JointType_Head], myJointArr[JointType_Neck], myMapper);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	delete[]myBodyArr;</span><br><span class="line">	myBodyFrame-&gt;Release();</span><br><span class="line">	myColorFrame-&gt;Release();</span><br><span class="line">	if (countFrame == 330) &#123;</span><br><span class="line">		break;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>5.关闭Kinect并退出</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">myMapper-&gt;Release();</span><br><span class="line">myDescription-&gt;Release();</span><br><span class="line">myColorReader-&gt;Release();</span><br><span class="line">myColorSource-&gt;Release();</span><br><span class="line">myBodyReader-&gt;Release();</span><br><span class="line">myBodySource-&gt;Release();</span><br><span class="line">mySensor-&gt;Close();</span><br><span class="line">mySensor-&gt;Release();</span><br><span class="line">return  0;</span><br></pre></td></tr></table></figure>

<p>6.函数draw()</p>
<p> 获取时间然后直接输出坐标。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">void    draw(Mat &amp; img, Joint &amp; r_1, Joint &amp; r_2, ICoordinateMapper * myMapper)</span><br><span class="line">&#123;</span><br><span class="line">	countFrame++;</span><br><span class="line">	if (r_1.TrackingState == TrackingState_Tracked )</span><br><span class="line">	&#123;</span><br><span class="line">		now = clock();</span><br><span class="line">		ofstream fout(&quot;01.txt&quot;, ios::app);</span><br><span class="line">		cout &lt;&lt; now;	</span><br><span class="line">		cout &lt;&lt; r_1.Position.X &lt;&lt; r_1.Position.Y &lt;&lt; r_1.Position.Z &lt;&lt; &#x27;\t ,&#x27;&lt;&lt;countFrame&lt;&lt;endl;</span><br><span class="line">		fout &lt;&lt; r_1.Position.X&lt;&lt;&#x27;,&#x27; &lt;&lt; r_1.Position.Y &lt;&lt; &#x27;,&#x27; &lt;&lt; r_1.Position.Z &lt;&lt;&#x27;,&#x27;&lt;&lt;now&lt;&lt; endl;</span><br><span class="line">		fout.close();</span><br><span class="line">	&#125;</span><br><span class="line">	else &#123;</span><br><span class="line">		now = clock();</span><br><span class="line">		ofstream fout(&quot;01.txt&quot;, ios::app);</span><br><span class="line">		cout &lt;&lt; now;</span><br><span class="line">		cout &lt;&lt; 0 &lt;&lt; 0 &lt;&lt;0 &lt;&lt; endl;</span><br><span class="line">		fout &lt;&lt; 0 &lt;&lt; &#x27;,&#x27; &lt;&lt;0 &lt;&lt; &#x27;,&#x27; &lt;&lt; 0 &lt;&lt; &#x27;,&#x27; &lt;&lt; now&lt;&lt;endl;</span><br><span class="line">		fout.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="完整代码如下："><a href="#完整代码如下：" class="headerlink" title="完整代码如下："></a>完整代码如下：</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line">//完整代码</span><br><span class="line">//其实本项目并没有用到opencv  我这是copy的别人的代码然后修改了一下。代码有很多和本项目无关的东西，等有时间删除精炼一下</span><br><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;opencv2\imgproc.hpp&gt;	//opencv头文件</span><br><span class="line">#include &lt;opencv2\calib3d.hpp&gt;</span><br><span class="line">#include &lt;opencv2\highgui.hpp&gt;</span><br><span class="line">#include &lt;Kinect.h&gt;	//Kinect头文件</span><br><span class="line">#include&lt;fstream&gt;</span><br><span class="line">#include&lt;ctime&gt;</span><br><span class="line">using   namespace   std;</span><br><span class="line">using   namespace   cv;</span><br><span class="line"></span><br><span class="line">int countFrame = 0;</span><br><span class="line">clock_t pre, now;</span><br><span class="line">clock_t begintime, endtime;</span><br><span class="line"></span><br><span class="line">void    draw(Mat &amp; img, Joint &amp; r_1, Joint &amp; r_2, ICoordinateMapper * myMapper);</span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">	begintime = clock();</span><br><span class="line">	IKinectSensor   * mySensor = nullptr;</span><br><span class="line">	GetDefaultKinectSensor(&amp;mySensor);</span><br><span class="line">	mySensor-&gt;Open();</span><br><span class="line"></span><br><span class="line">	IColorFrameSource   * myColorSource = nullptr;</span><br><span class="line">	mySensor-&gt;get_ColorFrameSource(&amp;myColorSource);</span><br><span class="line"></span><br><span class="line">	IColorFrameReader   * myColorReader = nullptr;</span><br><span class="line">	myColorSource-&gt;OpenReader(&amp;myColorReader);</span><br><span class="line"></span><br><span class="line">	int colorHeight = 0, colorWidth = 0;</span><br><span class="line">	IFrameDescription   * myDescription = nullptr;</span><br><span class="line">	myColorSource-&gt;get_FrameDescription(&amp;myDescription);</span><br><span class="line">	myDescription-&gt;get_Height(&amp;colorHeight);</span><br><span class="line">	myDescription-&gt;get_Width(&amp;colorWidth);</span><br><span class="line"></span><br><span class="line">	IColorFrame * myColorFrame = nullptr;</span><br><span class="line">	Mat original(colorHeight, colorWidth, CV_8UC4);</span><br><span class="line"></span><br><span class="line">	//**********************以上为ColorFrame的读取前准备**************************</span><br><span class="line"></span><br><span class="line">	IBodyFrameSource    * myBodySource = nullptr;</span><br><span class="line">	mySensor-&gt;get_BodyFrameSource(&amp;myBodySource);</span><br><span class="line">	IBodyFrameReader    * myBodyReader = nullptr;</span><br><span class="line">	myBodySource-&gt;OpenReader(&amp;myBodyReader);</span><br><span class="line">	int myBodyCount = 0;</span><br><span class="line">	myBodySource-&gt;get_BodyCount(&amp;myBodyCount);</span><br><span class="line">	IBodyFrame  * myBodyFrame = nullptr;</span><br><span class="line">	ICoordinateMapper   * myMapper = nullptr;</span><br><span class="line">	mySensor-&gt;get_CoordinateMapper(&amp;myMapper);</span><br><span class="line">	int fps = 0;</span><br><span class="line">	//**********************以上为BodyFrame以及Mapper的准备***********************</span><br><span class="line">	while (1)</span><br><span class="line">	&#123;</span><br><span class="line">		while (myColorReader-&gt;AcquireLatestFrame(&amp;myColorFrame) != S_OK);</span><br><span class="line">		myColorFrame-&gt;CopyConvertedFrameDataToArray(colorHeight * colorWidth * 4, original.data, ColorImageFormat_Bgra);</span><br><span class="line">		Mat copy = original.clone();        //读取彩色图像并输出到矩阵</span><br><span class="line">		</span><br><span class="line">		while (myBodyReader-&gt;AcquireLatestFrame(&amp;myBodyFrame) != S_OK); //读取身体图像</span><br><span class="line">		IBody   **  myBodyArr = new IBody *[myBodyCount];       //为存身体数据的数组做准备</span><br><span class="line">		if (fps != 0) &#123;</span><br><span class="line">			//cout &lt;&lt; fps &lt;&lt; endl;</span><br><span class="line">			fps++;</span><br><span class="line">			fps = fps % 30;</span><br><span class="line">			delete[]myBodyArr;</span><br><span class="line">			myBodyFrame-&gt;Release();</span><br><span class="line">			myColorFrame-&gt;Release();</span><br><span class="line">			continue;</span><br><span class="line">		&#125;</span><br><span class="line">		fps++;</span><br><span class="line">		for (int i = 0; i &lt; myBodyCount; i++)</span><br><span class="line">			myBodyArr[i] = nullptr;</span><br><span class="line">		if (myBodyFrame-&gt;GetAndRefreshBodyData(myBodyCount, myBodyArr) == S_OK)     //把身体数据输入数组</span><br><span class="line">			for (int i = 0; i &lt; myBodyCount; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				BOOLEAN     result = false;</span><br><span class="line">				if (myBodyArr[i]-&gt;get_IsTracked(&amp;result) == S_OK &amp;&amp; result) //先判断是否侦测到</span><br><span class="line">				&#123;</span><br><span class="line">					Joint   myJointArr[JointType_Count];</span><br><span class="line">					if (myBodyArr[i]-&gt;GetJoints(JointType_Count, myJointArr) == S_OK)   //如果侦测到就把关节数据输入到数组并画图</span><br><span class="line">					&#123;</span><br><span class="line">						draw(copy, myJointArr[JointType_Head], myJointArr[JointType_Neck], myMapper);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		delete[]myBodyArr;</span><br><span class="line">		myBodyFrame-&gt;Release();</span><br><span class="line">		myColorFrame-&gt;Release();</span><br><span class="line">		if (countFrame == 330) &#123;</span><br><span class="line">			break;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	myMapper-&gt;Release();</span><br><span class="line">	myDescription-&gt;Release();</span><br><span class="line">	myColorReader-&gt;Release();</span><br><span class="line">	myColorSource-&gt;Release();</span><br><span class="line">	myBodyReader-&gt;Release();</span><br><span class="line">	myBodySource-&gt;Release();</span><br><span class="line">	mySensor-&gt;Close();</span><br><span class="line">	mySensor-&gt;Release();</span><br><span class="line">	return  0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void    draw(Mat &amp; img, Joint &amp; r_1, Joint &amp; r_2, ICoordinateMapper * myMapper)</span><br><span class="line">&#123;</span><br><span class="line">	countFrame++;</span><br><span class="line">	if (r_1.TrackingState == TrackingState_Tracked )</span><br><span class="line">	&#123;</span><br><span class="line">		now = clock();</span><br><span class="line">		ofstream fout(&quot;01.txt&quot;, ios::app);</span><br><span class="line">		cout &lt;&lt; now;	</span><br><span class="line">		cout &lt;&lt; r_1.Position.X &lt;&lt; r_1.Position.Y &lt;&lt; r_1.Position.Z &lt;&lt; &#x27;\t ,&#x27;&lt;&lt;countFrame&lt;&lt;endl;</span><br><span class="line">		fout &lt;&lt; r_1.Position.X&lt;&lt;&#x27;,&#x27; &lt;&lt; r_1.Position.Y &lt;&lt; &#x27;,&#x27; &lt;&lt; r_1.Position.Z &lt;&lt;&#x27;,&#x27;&lt;&lt;now&lt;&lt; endl;</span><br><span class="line">		fout.close();</span><br><span class="line">	&#125;</span><br><span class="line">	else &#123;</span><br><span class="line">		now = clock();</span><br><span class="line">		ofstream fout(&quot;01.txt&quot;, ios::app);</span><br><span class="line">		cout &lt;&lt; now;</span><br><span class="line">		cout &lt;&lt; 0 &lt;&lt; 0 &lt;&lt;0 &lt;&lt; endl;</span><br><span class="line">		fout &lt;&lt; 0 &lt;&lt; &#x27;,&#x27; &lt;&lt;0 &lt;&lt; &#x27;,&#x27; &lt;&lt; 0 &lt;&lt; &#x27;,&#x27; &lt;&lt; now&lt;&lt;endl;</span><br><span class="line">		fout.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://huajiaohuixiang.gitee.io/2021/10/25/kinect%E8%8E%B7%E5%8F%96%E4%B8%89%E7%BB%B4%E5%9D%90%E6%A0%87/" data-id="ckvf4x3n10005okv1h8hh8dxb" data-title="kinect获取三维坐标" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Kinect/" rel="tag">Kinect</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/10/25/hexo%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          hexo博客搭建
        
      </div>
    </a>
  
  
    <a href="/2021/10/25/%E5%AE%89%E8%A3%85mysql%E3%80%81redus/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">安装mysql、redus</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/JetsonNano/" rel="tag">JetsonNano</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Kinect/" rel="tag">Kinect</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/frp/" rel="tag">frp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/windows/" rel="tag">windows</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" rel="tag">服务器</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BD%91%E7%BB%9C/" rel="tag">网络</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/" rel="tag">读书笔记</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/JetsonNano/" style="font-size: 10px;">JetsonNano</a> <a href="/tags/Kinect/" style="font-size: 10px;">Kinect</a> <a href="/tags/frp/" style="font-size: 10px;">frp</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/linux/" style="font-size: 10px;">linux</a> <a href="/tags/mysql/" style="font-size: 10px;">mysql</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/windows/" style="font-size: 10px;">windows</a> <a href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" style="font-size: 10px;">博客搭建</a> <a href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" style="font-size: 15px;">服务器</a> <a href="/tags/%E7%BD%91%E7%BB%9C/" style="font-size: 10px;">网络</a> <a href="/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/" style="font-size: 20px;">读书笔记</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/12/">December 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">November 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/10/">October 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/12/08/%E5%BC%A0%E7%BE%8E%E4%B8%BD/">张美丽</a>
          </li>
        
          <li>
            <a href="/2021/12/08/%E9%9D%92%E9%9D%92/">青青</a>
          </li>
        
          <li>
            <a href="/2021/12/08/%E5%B0%8F%E5%BC%A0%E5%92%8C%E5%B0%8F%E4%B8%BD/">小张和小丽</a>
          </li>
        
          <li>
            <a href="/2021/11/24/%E5%91%90%E5%96%8A/">呐喊</a>
          </li>
        
          <li>
            <a href="/2021/11/24/%E9%87%8E%E8%8D%89/">野草</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>